<template>
  <div>
    <el-dialog
      v-toggle-full
      class="base-dialog"
      :class="{ readonly: readOnly }"
      :custom-class="'panna-dialog_small'"
      :title="title"
      :append-to-body="true"
      :visible.sync="innerDialogFormVisible"
      :before-close="handleClose"
    >
      <div>
        <el-table :data="tableData" border style="width: 100%" height="220px">
          <el-table-column prop="detectionItemName" label="检测项目">
          </el-table-column>
          <el-table-column prop="methodStandard" label="方法标准编号">
          </el-table-column>
          <el-table-column prop="nationalStandardNum" label="产品标准编号">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div class="base-table-operation">
                <el-link
                  type="primary"
                  :disabled="scope.row.state == 1"
                  @click="selUnitELN(scope.row, scope.$index)"
                  >引用实验模板</el-link
                >
                <el-link
                  type="primary"
                  :disabled="scope.row.state == 1"
                  style="margin-left: 10px"
                  @click="update(scope.row, scope.$index)"
                  >编辑</el-link
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="base-dialog-footer">
        <el-button v-loading="submitLoad" type="primary" @click="onSubmit"
          >保 存</el-button
        >
        <el-button @click="handleClose">取 消</el-button>
      </span>
    </el-dialog>
    <ExperimentTemplateRecordAddDialog
      v-if="dialogFormVisibleAdd"
      ref="ExperimentTemplateRecordAddDialog"
      :dialog-form-visible.sync="dialogFormVisibleAdd"
      :table-data.sync="experimentTemplateData"
      :add-state.sync="addState"
      :pagination-experiment.sync="paginationExperiment"
      :experiment-template-data.sync="experimentTemplateData"
      @closeAddDialog="closeAddDialog"
    />
  </div>
</template>

<script>
import ExperimentTemplateRecordAddDialog from "../../../components/Eln/ExperimentTemplateRecordAddDialog.vue";
import { GetSampleRegistrationDetectionItem } from "../../../api/SampleManage/SampleRegistration";
import { Creates } from "../../../api/SampleManage/SampleInfoDetectionItemRecordSheet";
export default {
  name: "QuoteLabelDialog",
  components: {
    ExperimentTemplateRecordAddDialog,
  },
  props: {
    quoteLabelVisible: {
      type: Boolean,
      default: false,
    },
    readOnly: {
      type: Boolean,
      default: false,
    },
    sampleRegistrationId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      paginationExperiment: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
      addState: false,
      experimentTemplateData: [],
      rowIndex: -1,
      title: "引用实验记录单",
      innerDialogFormVisible: false,
      tableData: [],
      dialogFormVisibleAdd: false,
      experimentTemplateRecordIdList: [],
      experimentPagination: {
        pageIndex: 1,
        pageSize: 10,
      },
      submitLoad: false,
    };
  },
  computed: {},
  created() {
    this.innerDialogFormVisible = this.quoteLabelVisible;
    // 根据样品登记查询检测项目
    GetSampleRegistrationDetectionItem({ id: this.sampleRegistrationId }).then(
      (res) => {
        this.tableData = res.data;
      }
    );
  },
  methods: {
    update(row, index) {
      if (
        row.experimentTemplateData !== undefined &&
        row.experimentTemplateData !== null &&
        row.experimentTemplateData.length !== 0
      ) {
        this.experimentTemplateData = row.experimentTemplateData;
        this.rowIndex = index;
        this.addState = true;
        this.dialogFormVisibleAdd = true;
      } else {
        this.$message.error("请先引用实验记录单");
      }
    },
    closeAddDialog(val, pagination) {
      this.tableData[this.rowIndex].experimentTemplateData = [];
      this.tableData[this.rowIndex].experimentTemplateData = val;
      this.tableData[this.rowIndex].pagination = {};
      this.tableData[this.rowIndex].pagination = pagination;
      this.rowIndex = -1;
    },
    // 提交
    onSubmit() {
      // 引用数量
      var data = [];
      for (let i = 0; i < this.tableData.length; i++) {
        if (
          this.tableData[i].experimentTemplateData !== undefined &&
          this.tableData[i].experimentTemplateData !== null &&
          this.tableData[i].experimentTemplateData.length !== 0
        ) {
          for (
            let o = 0;
            o < this.tableData[i].experimentTemplateData.length;
            o++
          ) {
            if (
              this.tableData[i].experimentTemplateData[o].resetData !==
              undefined &&
              this.tableData[i].experimentTemplateData[o].resetData !== null
            ) {
              data.push({
                sampleInfoDetectionItemId:
                  this.tableData[i].sampleInfoDetectionItemId,
                experimentTemplateRecordTemplateId:
                  this.tableData[i].experimentTemplateData[o].id,
                experimentTemplateRecordId: 0,
                resetData:
                  this.tableData[i].experimentTemplateData[o].resetData,
                sampleRegistrationId: this.sampleRegistrationId,
              });
            } else {
              this.$message.error(
                "请编辑" +
                this.tableData[i].detectionItemName +
                "关联的实验记录单"
              );
              return false;
            }
          }
        }
      }

      this.submitLoad = true;
      Creates(data)
        .then((res) => {
          this.submitLoad = false;
          localStorage.removeItem('closedPageValue');
          this.handleClose();
        })
        .catch(() => {
          this.submitLoad = false;
        });
    },
    // 应用实验记录单
    selUnitELN(row, index) {
      this.rowIndex = index;
      if (row.experimentTemplateData !== undefined) {
        this.experimentTemplateData = row.experimentTemplateData;
      }
      this.dialogFormVisibleAdd = true;
    },
    // 关闭弹框
    handleClose() {
      this.innerDialogFormVisible = false;
      this.$emit("update:quoteLabelVisible", false);
      this.$emit("closeQuoteLabel");
    },
  },
};
</script>
